$(function () {
    $(function () {
        $.idcode.setCode(); //加载生成验证码方法


        $.validator.addMethod("checkPhone", function (value, ele, params) {
            return /^[1][3-9]\d{9}$/.test($(ele).val())
        }, "手机号不合法");

        //添加用户名校验规则
        $.validator.addMethod("checkUser", function (val, ele, params) {
            //value 输入的值
            //ele 那个元素
            return /^[a-z]\w{5,17}$/.test($(ele).val())
            // console.log(arguments);
        }, "用户名不合法");

        $("form").validate({
            // 规则
            rules: {
                uname: {
                    required: true,
                    rangelength: [6, 18],
                    checkUser: true,
                    remote: "./api/isReg"
                },
                upwd: {
                    required: true,
                    rangelength: [6, 18],
                },
                upwd2: {
                    equalTo: "#upwd"
                },
                uphone: {
                    checkPhone: true,
                }
            },
            // 消息提示
            messages: {
                uname: {
                    required: "请填写用户名",
                    rangelength: "长度在6-18之间",
                    remote: "该用户名已注册!"
                },
                upwd: {
                    required: "请填写密码",
                    rangelength: "长度在6-18之间",
                },
                upwd2: {
                    equalTo: "两次密码不一致"
                },
                uphone: {
                    checkPhone: "请输入正确手机号",
                }
            },
            // 提交
            submitHandler(from) {
                var dataStr = $(from).serialize();
                console.log(dataStr);


                var IsBy = $.idcode.validateCode()
                if (!IsBy) {
                    layer.msg('验证码错误,请重新输入');
                    return false;
                }


                $.ajax({
                    url: "./api/reg",
                    data: dataStr,
                    async: true,
                    type: "post",
                    dataType: "json",
                }).done(function (res) {
                    console.log(res);
                    // layer.msg(res.msg);
                    // layer.alert(res.msg);
                    alert(res.msg);
                    if (res.status >= 1) {
                        location.href = "./login.html";
                    }
                })

                return false;
            }

        })
    })
})